//
//  SISSLoginViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSLoginViewController.h"
#import "SISSProtocolViewController.h"
#import "SISSNavigationController.h"
#import "SISSLoginManger.h"
#import "SISSChangePasswordViewController.h"
#import "UIViewController+YYRPopupController.h"

@interface SISSLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UILabel *protocolLabel;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewCenterYConstraint;

@end

@implementation SISSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 429006199008271223 271223 430422199110015053 用户不存在
    // 431121198610107337
    // 431226198207176313
    // JZ042625 123456  无星级
    // JZ064523 123456
    // 452231199105022049  123456
    // 441424197506062266  123456
    // 510214197312221525
    // 320481199101086225 吴红
    
    NSString *userName = [SISSUserDefaultsTool objetForKey:CURRENTID];
    
    self.userNameTextField.text = userName;
    
    self.userNameTextField.delegate = self;
    // 设置清除按钮，就是那个叉叉X
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
//    self.userNameTextField.text = @"441522197303024123";
#ifdef DEBUG
    self.passwordTextField.text = @"123456";
    [self test];
#else
    
#endif
    
}

- (void)test {
    
    UIButton *one1Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [one1Btn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [one1Btn addTarget:self action:@selector(changeUserCount:) forControlEvents:UIControlEventTouchUpInside];
    one1Btn.tag = 100;
    [self.view addSubview:one1Btn];
    
    UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneBtn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [oneBtn addTarget:self action:@selector(changeUserCount:) forControlEvents:UIControlEventTouchUpInside];
    oneBtn.tag = 1;
    [self.view addSubview:oneBtn];
    
    
    UIButton *twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoBtn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [twoBtn addTarget:self action:@selector(changeUserCount:) forControlEvents:UIControlEventTouchUpInside];
    twoBtn.tag = 2;
    [self.view addSubview:twoBtn];
    
    UIButton *threeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [threeBtn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [threeBtn addTarget:self action:@selector(changeUserCount:) forControlEvents:UIControlEventTouchUpInside];
    threeBtn.tag = 3;
    [self.view addSubview:threeBtn];
    
    UIButton *fourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [fourBtn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [fourBtn addTarget:self action:@selector(changeUserCount:) forControlEvents:UIControlEventTouchUpInside];
    fourBtn.tag = 4;
    [self.view addSubview:fourBtn];
    
    [one1Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(oneBtn);
        make.right.equalTo(oneBtn.mas_left).offset(-16);
    }];
    
    [oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(self.view);
        }
        make.right.equalTo(twoBtn.mas_left).offset(-16);
    }];
    
    [twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(oneBtn);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [threeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(oneBtn);
        make.left.equalTo(twoBtn.mas_right).offset(16);
    }];
    [fourBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(oneBtn);
        make.left.equalTo(threeBtn.mas_right).offset(16);
    }];
    
    [oneBtn setTitle:@"刘苑" forState:UIControlStateNormal];
    [twoBtn setTitle:@"杜欣" forState:UIControlStateNormal];
    [threeBtn setTitle:@"甘海玲" forState:UIControlStateNormal];
    [fourBtn setTitle:@"杨爽爽" forState:UIControlStateNormal];
    [one1Btn setTitle:@"赵倩" forState:UIControlStateNormal];
}

- (void)changeUserCount:(UIButton *)btn {
    
    if (btn.tag == 1) {
        self.userNameTextField.text = @"360732199110151910";
    } else if (btn.tag == 2) {
        self.userNameTextField.text = @"441826xxxxxxxx1767";
    } else if (btn.tag == 3) {
        self.userNameTextField.text = @"450821198808120685";
    } else if (btn.tag == 4) {
        self.userNameTextField.text = @"410725198904054545";
    } else if (btn.tag == 100) {
        self.userNameTextField.text = @"610202xxxxxxxx4028";
    }
//    if (btn.tag == 1) {
//        self.userNameTextField.text = @"360732xxxxxxxx1910";
//    } else if (btn.tag == 2) {
//        self.userNameTextField.text = @"441826xxxxxxxx1767";
//    } else if (btn.tag == 3) {
//        self.userNameTextField.text = @"450821xxxxxxxx0685";
//    } else if (btn.tag == 4) {
//        self.userNameTextField.text = @"410725xxxxxxxx4545";
//    } else if (btn.tag == 100) {
//        self.userNameTextField.text = @"610202xxxxxxxx4028";
//    }
    
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    self.passwordTextField.text = @"";
    //在输入过程中 判断加上输入的字符 是否超过限定字数
    NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
    if (str.length > 18) {
        
        [SISSTools showSVWithTitle:@"不能超过18个字"];
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //返回一个BOOL值指明是否允许根据用户请求清除内容
    //可以设置在特定条件下才允许清除内容
    self.passwordTextField.text = @"";
    
    return YES;  
    
}

- (void)setupSubviews {
    [super setupSubviews];
    
    // 适配4.0屏幕
    self.headerViewHeightConstraint.constant = SCREEN_HEIGHT < 667 ? 150 : 200;
    self.headerViewCenterYConstraint.constant = SCREEN_HEIGHT < 667 ? -20 : 0;
    // 顶部视图的背景
//    self.headView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"矩形-4"]];
    
    // 交易通中介版label的圆角
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation.toValue = @(14);
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.titleLabel.layer addAnimation:animation forKey:@"cornerRadius"];
    
    // 登录按钮的边框
    self.loginButton.layer.borderWidth = 2;
    self.loginButton.layer.borderColor = [UIColor yyr_colorWithHex:0x16CDC9].CGColor;
    
}

- (IBAction)forgetPasswordButtonClickAction:(UIButton *)sender {
    
    [self popupAlertWithTitle:@"忘记密码" message:@"交易通密码和您的综合服务系统登录密码一致，如您忘记密码可以通过综合服务系统找回您的登录密码。" leftConfirmTitle:@"前往综合服务系统" rightConfirmTitle:@"取消" selectLeftBlock:^{
        SISSChangePasswordViewController *changePSDVC = [[SISSChangePasswordViewController alloc] init];
        changePSDVC.url = URL_CHANGE_PASSWORD;
        SISSNavigationController *nav = [[SISSNavigationController alloc] initWithRootViewController:changePSDVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
    } selectRightBlock:^{
        
    }];
    
    
}

/**
 登录按钮点击事件

 */
- (IBAction)loginButtonDidClick:(UIButton *)sender {
    NSLog(@"%@",BASE_URL);
    [self.view endEditing:YES];
    NSString *userName = self.userNameTextField.text;
    NSString *password = self.passwordTextField.text;
    
    if (self.userNameTextField.text.length == 0 ) {
        [self alertWithMessage:@"请您输入用户名。"];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [self alertWithMessage:@"请您输入密码。"];
        return;
    } else if (self.passwordTextField.text.length < 6) {
        [self alertWithMessage:@"您输入的密码不足6位。"];
        return;
    }
    
    [[SISSLoginManger shareInstance] loginWithUserName:userName password:password];
}



- (IBAction)protocolLabelTapAction:(UITapGestureRecognizer *)sender {
    
    SISSProtocolViewController *vc = [[SISSProtocolViewController alloc] init];
    SISSNavigationController *nav = [[SISSNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 登录失败的提示
- (void)alertWithMessage:(NSString *)message {
    
     NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName: COLOR(0x334A60)}];
    //初始化提示框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController setValue:attr forKey:@"attributedMessage"];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
